package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.adapter.CouponsRecordAdapter;
import com.hemaapp.yjnh.bean.CouponRecord;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

/**
 * 粮票的获取记录
 */
public class FoodStampGainListActivity extends BaseActivity {

    private ImageButton titleLeft;
    private TextView titleText;
    private Button titleRight;

    private TextView tvCouponsCount;
    private XtomRefreshLoadmoreLayout layout;
    private XtomListView listView;

    private CouponsRecordAdapter adapter;
    private ArrayList<CouponRecord> records = new ArrayList<CouponRecord>();
    private User user;
    private int page = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_coupons_get);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
        getCouponList();
    }

    @Override
    protected void findView() {
        titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
        titleText = (TextView) findViewById(R.id.title_text);
        titleRight = (Button) findViewById(R.id.title_right_btn);

        tvCouponsCount = (TextView) findViewById(R.id.tv_coupons_count);
        layout = (XtomRefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
        listView = (XtomListView) findViewById(R.id.listview);
    }

    @Override
    protected void getExras() {

    }

    @Override
    protected void setListener() {
        titleLeft.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleText.setText("获取记录");
        titleRight.setVisibility(View.GONE);
        layout.setOnStartListener(new StartListener());
    }

    private class StartListener implements OnStartListener {

        @Override
        public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
            page = 0;
            getCouponList();
        }

        @Override
        public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
            page++;
            getCouponList();
        }
    }

    private void getCouponList() {
        if (user != null) {
            getNetWorker().foodStampGainList(user.getToken(), page + "");
        }
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_GAIN_LIST:
                showProgressDialog("请稍候");
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_GAIN_LIST:
                cancelProgressDialog();
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_GAIN_LIST:
                String page = netTask.getParams().get("page");
                HemaPageArrayResult<CouponRecord> cResult = (HemaPageArrayResult<CouponRecord>) baseResult;
                if (cResult == null || cResult.getObjects() == null)
                    return;
                String count = user.getCouponGain();
                if (count != null) {
                    tvCouponsCount.setText(uuUtils.formatAfterDot2(count));
                } else {
                    tvCouponsCount.setText("0.00");
                }
                if ("0".equals(page)) {// 刷新
                    layout.refreshSuccess();
                    layout.setLoadmoreable(true);
                    records.clear();
                    records.addAll(cResult.getObjects());
                } else {// 更多
                    layout.loadmoreSuccess();
                    if (cResult.getObjects().size() > 0) {
                        records.addAll(cResult.getObjects());
                    } else {
                        layout.setLoadmoreable(false);
                        XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                    }
                }
                refreshData(-1);
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_GAIN_LIST:
                XtomToastUtil.showShortToast(mContext, "获取信息失败，" + baseResult.getMsg());
                break;
        }

    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case SCORE_GAIN_LIST:
                XtomToastUtil.showShortToast(mContext, "获取信息失败");
                refreshData(failedType);
                break;
        }
    }

    private void refreshData(int type) {
        if (adapter == null) {
            adapter = new CouponsRecordAdapter(mContext, records, 0);
            adapter.setEmptyString("没有获得过粮票");
            listView.setAdapter(adapter);
        } else {
            adapter.setEmptyString("没有获得过粮票");
            adapter.notifyDataSetChanged();
        }
        adapter.setFailtype(type);
    }

}
